<?php
namespace Admin\Controller;
use Tools\AdminController;

class SystemController extends AdminController {
    
    function basic(){
        $stmd = new \Model\SystemModel();
        
        //显示
        $web_name = $stmd -> where("system_name='web_name'") ->find();
        $web_lg   = $stmd -> where("system_name='web_logo'") ->find();
        $index_lg = $stmd -> where("system_name='index_logo'") ->find();
        
        
        //修改
        if(!empty($_POST)){
            
            //网站名称设置
            $name['system_name']  = 'web_name';
            $name['system_value'] = $_POST['web_name'];
            $n = $stmd -> create($name);
            if($n){
                $stmd -> save($n);       
            }else {
                $error = $stmd -> getError();
                $this -> redirect('basic','',2,$error);
            }
            
            //后台LOGO设置
            if($_FILES['web_logo']['error']<4){               
                //上传新文件
                $config = array(
                    'rootPath'  =>  "Admin/Public/uploads/",          //保存根路径
                    'maxSize'   =>  2000000,                            //上传的文件大小限制
                    'exts'      =>  array('jpg', 'gif', 'png', 'jpeg'), //允许上传的文件后缀
                );
                $upload = new \Think\Upload($config);
                //文件上传保存，返回附件的相关信息
                $up = $upload -> uploadOne($_FILES['web_logo']);
                if($up){
                                    
                    //删除旧LOGO文件
                    $supl = 'http://'.$_SERVER['HTTP_HOST'].'/Show/';
                    $link = ltrim($_POST['logo'],$supl);
                    unlink($link);

                    //图片上传成功后获得图片路径
                    $web_logo   = 'http://'.$_SERVER['HTTP_HOST'].'/Show/'.$upload -> rootPath.$up['savepath'].$up['savename'];
                }else {
                    $web_logo = $_POST['logo'];
                    $this -> redirect('basic','',2,$error);
                }
            }else {
                $web_logo = $_POST['logo'];
            }

            //后台LOGO存储
            $logo['system_name']  = "web_logo";
            $logo['system_value'] =  $web_logo;
            $l = $stmd -> create($logo);
            if($l){
                $stmd -> save($l);           
            }else {
                $error = $stmd -> getError();
                $this -> redirect('basic','',2,$error);
            }
            
              
            
            //网站LOGO设置
            if($_FILES['index_logo']['error']<4){               
                //上传新文件
                $config = array(
                    'rootPath'  =>  "Admin/Public/uploads/",          //保存根路径
                    'maxSize'   =>  2000000,                            //上传的文件大小限制
                    'exts'      =>  array('jpg', 'gif', 'png', 'jpeg'), //允许上传的文件后缀
                );
                $upload = new \Think\Upload($config);
                //文件上传保存，返回附件的相关信息
                $up = $upload -> uploadOne($_FILES['index_logo']);
                if($up){
                                    
                    //删除旧LOGO文件
                    $supl = 'http://'.$_SERVER['HTTP_HOST'].'/Show/';
                    $link = ltrim($index_lg['system_value'],$supl);
                    unlink($link);

                    //图片上传成功后获得图片路径
                    $index_logo   = 'http://'.$_SERVER['HTTP_HOST'].'/Show/'.$upload -> rootPath.$up['savepath'].$up['savename'];
                    $index['system_value'] = $index_logo;
                }else {
                    $error = $upload -> getError();
                    $this -> redirect('basic','',2,$error);
                }
            }else{
                $index['system_value'] = $_POST['i_logo'];
            }
            
            //网站LOGO存储
            $index['system_name'] = "index_logo";
            $d = $stmd -> create($index);
            if($d){
                if($stmd -> save($d)){
                    $this ->redirect('basic');
                }else{
                    $this  ->redirect('basic');   
                }
            }else {
                $error = $stmd -> getError();
                $this  -> alert($error);   
            }
            
        }
        
        $this -> assign('web_name',$web_name);
        $this -> assign('web_lg',$web_lg);
        $this -> assign('index_lg',$index_lg);
        $this -> display();
    }

}